<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 16.8.&nbsp; Nonblocking and Asynchronous I/O</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/team.gif" width="60" height="17" border="0" align="absmiddle"  alt="Team BBL"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=ch16lev1sec7.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<a href=ch16lev1sec9.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top"><a name="ch16lev1sec8"></a>
<h3 class="docSection1Title">16.8. Nonblocking and Asynchronous I/O</h3>
<p class="docText">Normally, the <tt>recv</tt> functions will block when no data is immediately available. Similarly, the <tt>send</tt> functions will block when there is not enough room in the socket's output queue to send the message. This behavior changes when the socket is in nonblocking mode. In this case, these functions will fail instead of blocking, setting <tt>errno</tt> to either <tt>EWOULDBLOCK</tt> or <tt>EAGAIN</tt>. When this happens, we can use either <tt>poll</tt> or <tt>select</tt> to determine when we can receive or transmit data.</P>
<p class="docText">The real-time extensions in the Single UNIX Specification include support for a generic asynchronous I/O mechanism. The socket mechanism has its own way of handling asynchronous I/O, but this isn't standardized in the Single UNIX Specification. Some texts refer to the classic socket-based asynchronous I/O mechanism as &quot;signal-based I/O&quot; to distinguish it from the asynchronous I/O mechanism in the real-time extensions.</P>
<p class="docText"><a name="idd1e126612"></a><a name="idd1e126617"></a><a name="idd1e126622"></a><a name="idd1e126625"></a><a name="idd1e126630"></a><a name="idd1e126635"></a><a name="idd1e126638"></a><a name="idd1e126641"></a><a name="idd1e126646"></a><a name="idd1e126651"></a><a name="idd1e126656"></a>With socket-based asynchronous I/O, we can arrange to be sent the <tt>SIGIO</tt> signal when we can read data from a socket or when space becomes available in a socket's write queue. Enabling asynchronous I/O is a two-step process.</p>
<div style="font-weight:bold"><ol class="docList" type="1"><LI><div style="font-weight:normal"><p class="docList">Establish socket ownership so signals can be delivered to the proper processes.</P></div></LI><li><div style="font-weight:normal"><p class="docList">Inform the socket that we want it to signal us when I/O operations won't block.</P></div></LI></ol></div>
<p class="docText">We can accomplish the first step in three ways.</P>
<div style="font-weight:bold"><ol class="docList" type="1"><li><div style="font-weight:normal"><p class="docList">Use the <tt>F_SETOWN</tt> command with <tt>fcntl</tt>.</P></div></li><LI><div style="font-weight:normal"><p class="docList">Use the <tt>FIOSETOWN</tt> command with <tt>ioctl</tt>.</P></div></LI><li><div style="font-weight:normal"><p class="docList">Use the <tt>SIOCSPGRP</tt> command with <tt>ioctl</tt>.</P></div></LI></ol></div>
<p class="docText">To accomplish the second step, we have two choices.</p>
<div style="font-weight:bold"><ol class="docList" type="1"><LI><div style="font-weight:normal"><p class="docList">Use the <tt>F_SETFL</tt> command with <tt>fcntl</tt> and enable the <tt>O_ASYNC</tt> file flag.</P></div></li><li><div style="font-weight:normal"><p class="docList">Use the <tt>FIOASYNC</tt> command with <tt>ioctl</tt>.</p></div></li></ol></div>
<p class="docText">We have several options, but they are not universally supported. <a class="docLink" href="#ch16fig21">Figure 16.21</a> summarizes the support for these options provided by the platforms discussed in this text. We show &#8226; where support is provided and <img src=images/ent/U2020.GIF border=0> where support depends on the particular domain. For example, on Linux, the UNIX domain sockets don't support <tt>FIOSETOWN</tt> or <tt>SIOCSPGRP</tt>.</P>
<a name="ch16fig21"></a><p><table cellspacing="0" class="allBorders" border="1" RULES="groups" cellpadding="5"><caption><H5 class="docTableTitle">Figure 16.21. Socket asynchronous I/O management commands</h5></caption><colgroup><col width="200"><col width="60"><col width="60"><col width="60"><col width="60"><col width="60"></colgroup><thead><TR><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="middle"><p class="docText"><span class="docEmphRoman">Mechanism</span></p></th><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="top"><p class="docText"><span class="docEmphRoman">POSIX.1</span></p></th><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="top"><p class="docText"><span class="docEmphRoman">FreeBSD 5.2.1</span></p></th><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="top"><p class="docText"><span class="docEmphRoman">Linux 2.4.22</span></p></th><th class="rightBorder bottomBorder thead" scope="col" align="center" valign="top"><p class="docText"><span class="docEmphRoman">Mac OS X 10.3</span></p></th><th class="bottomBorder thead" scope="col" align="center" valign="top"><p class="docText"><span class="docEmphRoman">Solaris 9</span></p></th></tr></thead><tr><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>fcntl(fd, F_SETOWN, pid)</tt></p></td><td class="rightBorder" align="center" valign="top"><p class="docText">&#8226;</p></td><td class="rightBorder" align="center" valign="top"><p class="docText">&#8226;</p></td><TD class="rightBorder" align="center" valign="top"><p class="docText">&#8226;</P></td><TD class="rightBorder" align="center" valign="top"><p class="docText">&#8226;</P></TD><td class="docTableCell" align="center" valign="top"><p class="docText">&#8226;</P></TD></TR><tr><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>ioctl(fd, FIOSETOWN, pid)</tt></p></TD><TD class="rightBorder" align="left" valign="top">&nbsp;</TD><td class="rightBorder" align="center" valign="top"><p class="docText">&#8226;</P></TD><td class="rightBorder" align="center" valign="top"><p class="docText"><img src=images/ent/U2020.GIF border=0></P></TD><td class="rightBorder" align="center" valign="top"><p class="docText">&#8226;</p></td><td class="docTableCell" align="center" valign="top"><p class="docText">&#8226;</P></td></TR><tr><TD class="rightBorder bottomBorder" align="left" valign="top"><p class="docText"><tt>ioctl(fd, SIOCSPGRP, pid)</tt></p></td><td class="rightBorder bottomBorder" align="left" valign="top">&nbsp;</td><td class="rightBorder bottomBorder" align="center" valign="top"><p class="docText">&#8226;</p></td><td class="rightBorder bottomBorder" align="center" valign="top"><p class="docText"><img src=images/ent/U2020.GIF border=0></p></td><td class="rightBorder bottomBorder" align="center" valign="top"><p class="docText">&#8226;</p></td><td class="bottomBorder" align="center" valign="top"><p class="docText">&#8226;</p></td></tr><TR><TD class="rightBorder" align="left" valign="top"><p class="docText"><tt>fcntl(fd, F_SETFL, flags|O_ASYNC)</tt></p></TD><TD class="rightBorder" align="left" valign="top">&nbsp;</TD><td class="rightBorder" align="center" valign="top"><p class="docText">&#8226;</P></TD><TD class="rightBorder" align="center" valign="top"><p class="docText">&#8226;</p></TD><td class="rightBorder" align="center" valign="top"><p class="docText">&#8226;</P></TD><TD class="docTableCell" align="left" valign="top">&nbsp;</td></TR><TR><td class="rightBorder" align="left" valign="top"><p class="docText"><tt>ioctl(fd, FIOASYNC, &amp;n);</tt></P></TD><td class="rightBorder" align="left" valign="top">&nbsp;</td><td class="rightBorder" align="center" valign="top"><p class="docText">&#8226;</p></TD><td class="rightBorder" align="center" valign="top"><p class="docText">&#8226;</P></td><TD class="rightBorder" align="center" valign="top"><p class="docText">&#8226;</p></td><td class="docTableCell" align="center" valign="top"><p class="docText">&#8226;</p></td></tr></table></p><br>

<a href="17021535.html"><img src="images/pixel.gif" alt="" width="1" height="1" border="0"></a><ul></ul></td></tr></table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/team.gif" width="60" height="17" border="0" align="absmiddle"  alt="Team BBL"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=ch16lev1sec7.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<a href=ch16lev1sec9.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
</body></html><br>
<table width="100%" cellspacing="0" cellpadding="0"
style="margin-top: 0pt; border-collapse: collapse;"> 
<tr> <td align="right" style="background-color=white; border-top: 1px solid gray;"> 
<a href="http://www.zipghost.com/" target="_blank" style="font-family: Tahoma, Verdana;
 font-size: 11px; text-decoration: none;">The CHM file was converted to HTM by Trial version of <b>ChmD<!--117-->ecompiler</b>.</a>
</TD>
</TR><tr>
<td align="right" style="background-color=white; "> 
<a href="http://www.etextwizard.com/download/cd/cdsetup.exe" target="_blank" style="font-family: Tahoma, Verdana;
 font-size: 11px; text-decoration: none;">Download <b>ChmDec<!--117-->ompiler</b> at: http://www.zipghost.com</a>
</TD></tr></table>
